package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author Deng Zaojian
 * @program: micro-mall
 * @description:
 * @create: 2021-09-29 11:27
 **/

@RestController
@RequestMapping("/shopping")
public class ShoppingSearchController {

    @Reference(timeout = 6000, retries = 0, check = true)
    IProductService productService;

    @Anoymous
    @GetMapping("/goods")
    public ResponseData getGoods(AllProductRequest request) {
        AllProductResponse response = productService.getAllProduct(request);
   

        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response);
        }

        return new ResponseUtil().setErrorMsg(response.getMsg());

    }


    @Anoymous
    @GetMapping("/product/{id}")
    public ResponseData getProductDetail(@PathVariable(value = "id") Long id) {
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);
        ProductDetailResponse response = null;
        try {
            response = productService.getProductDetail(request);
        } catch (Exception e) {
            System.out.println("超时了 ");

        }
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getProductDetailDto());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }

    @Anoymous
    @GetMapping("/recommend")
    public ResponseData getRecommend() {

        RecommendResponse response = productService.getRecommendGoods();
        
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil().setData(response.getPanelContentItemDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());

    }

}
